public class Character {
  String name;
  ArrayList<InvSlot> inventory;

  int baseVit = 0;
  int baseStn = 0;
  int baseIns = 0;
  int baseSpd = 0;
  int baseFoc = 50;
  int baseHP = 100;


  int myVit = 0;
  int myStn = 0;
  int myIns = 0;
  int mySpd = 0;
  int myFoc = 50;
  int myHP = 100;
  int myLvl = 1;
  int numHPPots = 1;
  int numFocPots = 2;
  InvSlot<Helm> headslot = new InvSlot<Helm>();
  InvSlot<Gloves> handslot = new InvSlot<Gloves>();
  InvSlot<Body> bodyslot = new InvSlot<Body>();
  InvSlot<Pants> legslot = new InvSlot<Pants>();
  InvSlot<Shoes> footslot = new InvSlot<Shoes>();
  InvSlot<Weapon> weaponslot = new InvSlot<Weapon>();
  InvSlot<Accessory> offslot = new InvSlot<Accessory>();
  ArrayList<InvSlot> equipped = new ArrayList<InvSlot>();
  int[] stats = {myVit, myStn, myIns, mySpd, myFoc, myHP, myLvl};
  ArrayList<String> abilities = new ArrayList<String>();
  ArrayList<Buff> buffs = new ArrayList<Buff>();

  public Character() {
    name = "Default";
    inventory = new ArrayList<InvSlot>();
    /*equipped.add(headslot);
    equipped.add(handslot);
    equipped.add(bodyslot);
    equipped.add(legslot);
    equipped.add(footslot);
    equipped.add(offslot);
    equipped.add(weaponslot);
    equip(new Body());
    equip(new Helm());
    equip(new Gloves());
    equip(new Pants());
    equip(new Shoes());
    equip(new Accessory());
    equip(new Weapon());*/
  }

  public void attack(Character target) {
    target.takeDmg(weaponslot.getPiece().calcDmg() + myStn);
  }

  public void takeDmg(int dmg) {
    myHP = myHP - dmg;
  } 

  public int[] getStats() {
    return stats;
  }

  public ArrayList<String> getAbilities() {
    return abilities;
  }

  public void useHPPotion() {
    if (numHPPots > 0) {
      myHP = myHP + 20;
      numHPPots--;
    }
  }

  public void useFocPotion() {
    if (numFocPots > 0) {
      myFoc = myFoc + 20;
      numFocPots--;
    }
  }

  public ArrayList<InvSlot> getEquipped() {
    return equipped;
  }

  public boolean useAbility(String s, Character target) {
    int[] a;
    AbilityRetriever ab = new AbilityRetriever();
    a = ab.getEffect(s);
    if (myFoc < a[3]) {
  
      return false;
    }
    myFoc = myFoc - a[3];
    if (a[0] == 0) {
      target.takeDmg(a[1] + this.myIns);
      if (target.myHP > target.baseHP) {
        target.myHP = target.baseHP;
      }
    }
    if (a[0] == 1) {
      target.takeDmg(-1 * (a[1] + this.myIns));
      if (target.myHP > target.baseHP) {
        target.myHP = target.baseHP;
      }
    }
    if (a[0] == 2) {
      Buff b = new Buff(a[0], a[1], a[2], a[3], myVit);
      buffs.add(b);
    }
    if (a[0] == 3) {
      Buff b = new Buff(a[0], a[1], a[2], a[3], myStn);
      buffs.add(b);
    }
    if (a[0] == 4) {
      Buff b = new Buff(a[0], a[1], a[2], a[3], myIns);
      buffs.add(b);
    }
    if (a[0] == 5) {
      Buff b = new Buff(a[0], a[1], a[2], a[3], mySpd);
      buffs.add(b);
    }
    if (a[0] == 6) {
      Buff b = new Buff(a[0], a[1], a[2], a[3], myHP);
      target.buffs.add(b);
    }
    return true;
  }

  public void checkBuffs() {
    Buff b;
    for (int i=0;i<buffs.size();i++) {
      b = buffs.get(i);
      if (b.stat == 2) {
        if (b.duration <= 0) {
          buffs.remove(i);
          myVit = b.origStat;
        }
        else {
          if (b.duration == b.maxTime) {
            myVit = myVit + b.effect;
            b.duration--;
          }
          else {
            b.duration--;
          }
        }
      }
      if (b.stat == 3) {
        if (b.duration <= 0) {
          buffs.remove(i);
          myStn = b.origStat;
        }
        else {
          if (b.duration == b.maxTime) {
            myStn = myStn + b.effect;
            b.duration--;
          }
          else {
            b.duration--;
          }
        }
      }
      if (b.stat == 4) {
        if (b.duration <= 0) {
          buffs.remove(i);
          myIns = b.origStat;
        }
        else {
          if (b.duration == b.maxTime) {
            myIns = myIns + b.effect;
            b.duration--;
          }
          else {
            b.duration--;
          }
        }
      }
      if (b.stat == 5) {
        if (b.duration <= 0) {
          buffs.remove(i);
          mySpd = b.origStat;
        }
        else {
          if (b.duration == b.maxTime) {
            mySpd = mySpd + b.effect;
            b.duration--;
          }
          else {
            b.duration--;
          }
        }
      }
      if (b.stat == 6) {
        if (b.duration <= 0) {
          buffs.remove(i);
        }
        else {
          myHP = myHP + b.effect;
          if (myHP > baseHP) {
            myHP = baseHP;
          }
          b.duration--;
        }
      }
    }
  }

  public void calcStats() {
    for (int i=0;i<equipped.size();i++) {
      if (equipped.get(i).getPiece() != null) {
        Armor current =(Armor)equipped.get(i).getPiece();
        baseVit += current.getVit();
        baseStn += current.getStn();
        baseIns += current.getIns();
        baseSpd += current.getSpd();
      }
    }
    stats[0] = myVit;
    stats[1] = myStn;
    stats[2] = myIns;
    stats[3] = mySpd;
    stats[4] = myFoc;
    stats[5] = myHP;
    stats[6] = myLvl;
    myHP = myHP + (baseVit*10);
  }
  public void equip(Armor e) {
    if (e.getClass().getName().equals("sonny$Body")) {
      bodyslot.setItem((Body)e);
    }
    if (e.getClass().getName().equals("sonny$Helm")) {
      headslot.setItem((Helm)e);
    }
    if (e.getClass().getName().equals("sonny$Pants")) {
      legslot.setItem((Pants)e);
    }
    if (e.getClass().getName().equals("sonny$Gloves")) {
      handslot.setItem((Gloves)e);
    }
    if (e.getClass().getName().equals("sonny$Shoes")) {
      footslot.setItem((Shoes)e);
    }
    if (e.getClass().getName().equals("sonny$Weapon")) {
      weaponslot.setItem((Weapon)e);
    }
    if (e.getClass().getName().equals("sonny$Accessory")) {
      offslot.setItem((Accessory)e);
    }
    calcStats();
  }
  
   public int getHP() {
    return myHP;
  }
  
  public int getFoc() {
    return myFoc;
  }
}

